package com.dq.controller;

import com.dq.mapper.ApplyForGiftMapper;
import com.dq.mapper.GoodsMapper;
import com.dq.mapper.UserMapper;
import com.dq.pojo.ApplyForGift;
import com.dq.pojo.Goods;
import com.dq.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    JavaMailSenderImpl mailSender;

    @Autowired
    ApplyForGiftMapper applyForGiftMapper;

    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser(){
        return new User();
    }


    @RequestMapping("/user")
    public String user(Model model){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String id = userDetails.getUsername();
        List<Goods> goods = goodsMapper.getGoodsByDelegateId(id);
        model.addAttribute("goods",goods);
        User user = userMapper.getUserById(id);
        model.addAttribute("user",user);

        List<ApplyForGift> afgs = applyForGiftMapper.getAFGById(id);

        model.addAttribute("afgs",afgs);
        HashMap picMap = new HashMap();
        HashMap nameMap1 = new HashMap();
        for (ApplyForGift gift : afgs) {

            Goods goodById = goodsMapper.getGoodById(gift.getId());

            picMap.put(gift.getId(),goodById.getPic());
            nameMap1.put(gift.getId(),goodById.getProductName());

        }
        model.addAttribute("picMap",picMap);
        model.addAttribute("nameMap1",nameMap1);

        model.addAttribute("id",id);
        return "/web1/userIndex";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/web1/login";
    }

    @RequestMapping("/login.html")
    public String toLoginHtml(){
        return "/web1/login";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "/web1/register";
    }

    @RequestMapping("/register")
    public String register(@RequestParam(value = "id",required = false)String id,
                           @RequestParam(value = "name",required = false)String name,
                           @RequestParam(value = "file",required = false) MultipartFile file,
                           @RequestParam(value = "role",required = false)String role,
                           @RequestParam(value = "email",required = false)String email,
                           @RequestParam(value = "password",required = false)String password,
                           @RequestParam(value = "address",required = false)String address,
                           @RequestParam(value = "money",required = false)String money) throws IOException {

        String path = "D:/Allworkspace/ideaSPACE/mysign/src/main/resources/static/images/user";
        System.out.println(path);
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        StringBuffer sb=new StringBuffer("/images/user/");
        sb.append(file.getOriginalFilename());
        String picPath=sb.toString();

        file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));

        userMapper.insertUser(new User(id,name,password,Double.parseDouble(money),role,address,email,picPath));
        //return "/web1/register";
        return "redirect:/toLogin";
    }

    @RequestMapping("/sendMail")
    public String sendMail(@RequestParam("subject")String subject,@RequestParam("content")String content,
                           @RequestParam("receiver")String receiver,@RequestParam("userEmail")String email
    ){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(content);

        message.setTo(receiver);
        message.setFrom(email);
        mailSender.send(message);
        return "redirect:/user";
    }

    @RequestMapping("/updateUser")
    public String updateUser(User user){
        userMapper.updateUser(user);
        return "redirect:/user";
    }

    @RequestMapping("/toForgotPassword")
    public String toForgotpPassword(){
        return "/web1/forgot-password";
    }

    @RequestMapping("/toMessages")
    public String toMessage(){
        return "/web1/messages";
    }

    @RequestMapping("/findAccount")
    public String findAccount(@RequestParam(value = "id",required = false) String id){
        User userById = userMapper.getUserById(id);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("找回账号信息");
        String pwd="123456"+(int)(Math.random()*100);
        message.setText("您的账号密码已重置为："+pwd);
        System.out.println("您的账号密码已重置为：" + pwd);

        userById.setPwd(pwd);
        userMapper.updateUser(userById);


        message.setTo(userById.getEmail());
        message.setFrom("1548436972@qq.com");
        mailSender.send(message);
        return "redirect:/toLogin";
    }

    @RequestMapping("/finishThis/{id}")
    public String finishThis(@PathVariable("id")String id){
        applyForGiftMapper.updateAFG(id);
        return "redirect:/user";
    }


}
